package com.glorysoft.sunnypda.ui.reportrepair

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.glorysoft.sunnypda.BR
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseActivity
import com.glorysoft.sunnypda.base.Constant.Companion.CURRENT_USER_NAME
import com.glorysoft.sunnypda.base.Constant.Companion.isReportRepairActivity
import com.glorysoft.sunnypda.databinding.ActivityReportRepairBinding
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.ui.login.LoginActivity
import kotlinx.android.synthetic.main.activity_report_repair.etEqpWork
import kotlinx.android.synthetic.main.activity_report_repair.tvFinish
import kotlinx.android.synthetic.main.activity_report_repair.tvRunning
import kotlinx.android.synthetic.main.activity_report_repair.tvUnFinish

/**
 * FileName: ReportRepairActivity
 * Author: XJH
 * Date: 2022/1/7 16:05
 * Description:
 * History:
 */
class ReportRepairActivity : BaseActivity<ReportRepairViewModel,ActivityReportRepairBinding>(),
    View.OnClickListener {

    var status : Int = 0

    override fun layoutId(): Int {
        return R.layout.activity_report_repair
    }

    override fun initVariableId(): Int {
        return BR.reportRepairViewModel
    }

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        isReportRepairActivity = true
        if (CURRENT_USER_NAME == "") {
            viewModel.defUI.toastEvent.value = ToastMessageBean("请先登录",false)
            val intent = Intent(this,LoginActivity::class.java)
            intent.putExtra("isManager","1")
            startActivity(intent)
            finish()
            return
        }

        setTitle(getString(R.string.fault_maintenance))
        tvUnFinish.setOnClickListener(this)
        tvFinish.setOnClickListener(this)
        tvRunning.setOnClickListener(this)

        viewModel.initRepairForUser(status,"")

        initEdit(etEqpWork,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.initRepairForEqp(status,s)
            }

        })
        viewModel.defUI.callObserve.observe(this){
            tvUnFinish.text =
                getString(R.string.unfinished) + "(" + viewModel.unFinishList.size + ")"
            tvFinish.text = getString(R.string.finish) + "(" + viewModel.finishList.size + ")"
            tvRunning.text = getString(R.string.in_process) + "(" + viewModel.runningList.size + ")"
        }

    }

    override fun initData() {
        viewModel.defUI.startActivity.observe(this){
            val intent = Intent(this,ReportRepairInfoActivity::class.java)
            intent.putExtras(it)
            startActivityForResult(intent,0)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode){
            RESULT_OK ->{
                viewModel.initRepairForUser(status,etEqpWork.text.toString())
            }
            RESULT_CANCELED ->{
                viewModel.initRepairForUser(status,"")
            }
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.tvUnFinish ->{
                status = 0
                updateUI()
            }
            R.id.tvFinish ->{
                status = 2
                updateUI()
            }
            R.id.tvRunning ->{
                status = 1
                updateUI()
            }
        }

    }

    private fun updateUI() {
        when(status){
            0 ->{
                tvUnFinish.setBackgroundColor(
                    resources.getColor(R.color.free_ui_blue_sky_theme_color)
                )
                tvUnFinish.setTextColor(resources.getColor(R.color.white))
                tvFinish.setBackgroundColor(resources.getColor(R.color.white))
                tvRunning.setBackgroundColor(resources.getColor(R.color.white))
                tvFinish.setTextColor(resources.getColor(R.color.black))
                tvRunning.setTextColor(resources.getColor(R.color.black))
            }
            2 ->{
                tvFinish.setBackgroundColor(
                    resources.getColor(R.color.free_ui_blue_sky_theme_color)
                )
                tvFinish.setTextColor(resources.getColor(R.color.white))
                tvUnFinish.setBackgroundColor(resources.getColor(R.color.white))
                tvRunning.setBackgroundColor(resources.getColor(R.color.white))
                tvUnFinish.setTextColor(resources.getColor(R.color.black))
                tvRunning.setTextColor(resources.getColor(R.color.black))
            }
            1 ->{
                tvRunning.setBackgroundColor(
                    resources.getColor(R.color.free_ui_blue_sky_theme_color)
                )
                tvRunning.setTextColor(resources.getColor(R.color.white))
                tvUnFinish.setBackgroundColor(resources.getColor(R.color.white))
                tvFinish.setBackgroundColor(resources.getColor(R.color.white))
                tvUnFinish.setTextColor(resources.getColor(R.color.black))
                tvFinish.setTextColor(resources.getColor(R.color.black))
            }
        }

        viewModel.updateList(status,etEqpWork.text.toString())

    }

    override fun onDestroy() {
        super.onDestroy()
        isReportRepairActivity = false
    }

}